package org.jeecg.modules.order.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.order.entity.AddtimeOrder;
import org.jeecg.modules.order.entity.Order;
import org.jeecg.modules.order.service.IAddtimeOrderService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.order.service.IOrderService;
import org.jeecg.modules.recharge.entity.Balance;
import org.jeecg.modules.recharge.entity.OrderRecharge;
import org.jeecg.modules.recharge.service.IBalanceService;
import org.jeecg.modules.system.util.DateUtil;
import org.jeecg.modules.system.util.MenJinUtil;
import org.jeecg.modules.zixishi.entity.Quyu;
import org.jeecg.modules.zixishi.entity.Seat;
import org.jeecg.modules.zixishi.entity.Zixishi;
import org.jeecg.modules.zixishi.service.IQuyuService;
import org.jeecg.modules.zixishi.service.ISeatService;
import org.jeecg.modules.zixishi.service.IZixishiService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.modules.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 加时订单
 * @Author: jeecg-boot
 * @Date:   2024-06-20
 * @Version: V1.0
 */
@Api(tags="加时订单")
@RestController
@RequestMapping("/order/addtimeOrder")
@Slf4j
public class AddtimeOrderController extends JeecgController<AddtimeOrder, IAddtimeOrderService> {
	@Autowired
	private IAddtimeOrderService addtimeOrderService;
	@Autowired
	private IBalanceService	balanceService;
	@Autowired
	private IOrderService orderService;
	@Autowired
	private ISeatService seatService;
	@Autowired
	private IQuyuService quyuService;
	@Autowired
	private IZixishiService zixishiService;
	@Autowired
	private RedisUtil redisUtil;
	
	/**
	 * 分页列表查询
	 *
	 * @param addtimeOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "加时订单-分页列表查询")
	@ApiOperation(value="加时订单-分页列表查询", notes="加时订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AddtimeOrder>> queryPageList(AddtimeOrder addtimeOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AddtimeOrder> queryWrapper = QueryGenerator.initQueryWrapper(addtimeOrder, req.getParameterMap());
		Page<AddtimeOrder> page = new Page<AddtimeOrder>(pageNo, pageSize);
		System.out.println(queryWrapper.getCustomSqlSegment());
		IPage<AddtimeOrder> pageList = addtimeOrderService.pageList(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param addtimeOrder
	 * @return
	 */
	@AutoLog(value = "加时订单-添加")
	@ApiOperation(value="加时订单-添加", notes="加时订单-添加")
	//@RequiresPermissions("org.jeecg.modules:sr_addtime_order:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AddtimeOrder addtimeOrder) {
		addtimeOrder.setOrderNum("" + FillRuleUtil.executeRule("shop_order_num", new JSONObject()));
		try {
			return addtimeOrderService.addByAdmin(addtimeOrder);
		}catch (Exception e){
			return Result.error(e.getMessage());
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param addtimeOrder
	 * @return
	 */
	@AutoLog(value = "加时订单-编辑")
	@ApiOperation(value="加时订单-编辑", notes="加时订单-编辑")
	//@RequiresPermissions("org.jeecg.modules:sr_addtime_order:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AddtimeOrder addtimeOrder) {
		addtimeOrderService.updateById(addtimeOrder);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "加时订单-通过id删除")
	@ApiOperation(value="加时订单-通过id删除", notes="加时订单-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:sr_addtime_order:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		addtimeOrderService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "加时订单-批量删除")
	@ApiOperation(value="加时订单-批量删除", notes="加时订单-批量删除")
	//@RequiresPermissions("org.jeecg.modules:sr_addtime_order:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.addtimeOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "加时订单-通过id查询")
	@ApiOperation(value="加时订单-通过id查询", notes="加时订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AddtimeOrder> queryById(@RequestParam(name="id",required=true) String id) {
		AddtimeOrder addtimeOrder = addtimeOrderService.getById(id);
		if(addtimeOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(addtimeOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param addtimeOrder
    */
    //@RequiresPermissions("org.jeecg.modules:sr_addtime_order:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AddtimeOrder addtimeOrder) {
		String exportFieldsList = this.getExportFields(AddtimeOrder.class,"addtimeorder:");
//		return super.exportXls(request, orderRecharge, OrderRecharge.class, "充值订单",exportFieldsList);
        return super.exportXls(request, addtimeOrder, AddtimeOrder.class, "加时订单",exportFieldsList);
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("sr_addtime_order:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AddtimeOrder.class);
    }

	 @SneakyThrows
	 @AutoLog(value = "订单-添加")
	 @ApiOperation(value="订单-添加", notes="订单-添加")
	 //@RequiresPermissions("org.jeecg.modules:sr_order:add")
	 @PostMapping(value = "/doOrder")
	 public Result<AddtimeOrder> doOrder(@RequestBody AddtimeOrder order) {

		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 order.setSysUserId(sysUser.getId());
		 order.setPayStatus("0");
		 order.setOrderNum("" + FillRuleUtil.executeRule("shop_order_num", new JSONObject()));
		 addtimeOrderService.save(order);
		 return Result.OK(order);
	 }

	 @ApiOperation(value="订单支付使用余额", notes="订单支付使用余额")
	 @PostMapping(value = "/payorder")
	 public Result<String> payOrder(@RequestBody Map paramMap) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String orderNum = "" + paramMap.get("orderNum");
		 return addtimeOrderService.payOrder4Balance(sysUser.getId(),orderNum);
	 }
}
